#pragma once
#include<iostream>
#include<string.h>
#include<unistd.h>
#include<sys/epoll.h>
#include"nocopy.hpp"

using std::cout;
using std::endl;

class Epoller:public nocopy
{
    const static int default_size=128;
public:
    Epoller()
    {
        _epfd=epoll_create(default_size);
        if(_epfd==-1)
        {
            cout<<"epoll create fail,strerror:"<<strerror(errno)<<endl;
        }
        else 
        {
            cout<<"epoll create successfully,epfd is:"<<_epfd<<endl;
        }
    }

    int EpollWait(struct epoll_event events[],int num) 
    {
        int n=epoll_wait(_epfd,events,num,/*_timeout*/-1);//0：非阻塞等待    -1：阻塞等待
        return n;
    }

    void EpollCtrl(int op,int sockfd,uint32_t event)
    {
        struct epoll_event events;
        events.events=event;
        events.data.fd=sockfd;
        int n;

        if(op==EPOLL_CTL_DEL)
            n=epoll_ctl(_epfd,op,sockfd,nullptr);
        else 
            n=epoll_ctl(_epfd,op,sockfd,&events);
        
        if(n==-1)
            cout<<"epoll control fail..."<<endl;
        else    
            cout<<"epoll control successfully"<<endl;
    }

    ~Epoller()
    {
        if(_epfd>=0)
            close(_epfd);
    }
private:
    int _epfd;
    int _timeout=1000;//1秒
};